package com.uzai.console.service.godapi.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.uzai.common.utils.EsIndexName;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.ES.ElasticsearchUtilGroupMsg;
import com.uzai.console.dto.fd.fdfollowgroupconfig.FdFollowGroupConfigQuery;
import com.uzai.console.dto.fd.fdsendjob.FdSendJobQuery;
import com.uzai.console.dto.god.pyq.PyqReportDto;
import com.uzai.console.dto.pyq.pyqautoconfig.PyqAutoConfigQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.FdDevicePlanGroupMapper;
import com.uzai.console.mapper.FdDevicePlanMapper;
import com.uzai.console.mapper.FdFollowGroupConfigMapper;
import com.uzai.console.mapper.FdSendJobMapper;
import com.uzai.console.service.godapi.GodApiService;
import com.uzai.console.vo.god.pyq.PyqReportVo;
import io.swagger.annotations.ApiModelProperty;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Auther: liuqi
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class GodApiServiceImpl implements GodApiService {

    private static Logger logger = LoggerFactory.getLogger(GodApiServiceImpl.class);

    @Autowired
    private ElasticsearchUtilGroupMsg elasticsearchUtilGroupMsg;
    @Autowired
    private FdSendJobMapper fdSendJobMapper;
    @Autowired
    private FdFollowGroupConfigMapper fdFollowGroupConfigMapper;
    @Autowired
    private FdDevicePlanMapper fdDevicePlanMapper;
    @Autowired
    private FdDevicePlanGroupMapper fdDevicePlanGroupMapper;

    /**
     * god平台朋友圈统计
     * @author liuqi
     * @date 2022/11/23 11:38
     * @param
     * @return pyqReportDto
     */
    public PyqReportVo godPyqReport(PyqReportDto pyqReportDto){
        PyqReportVo pyqReportVo = new PyqReportVo();

        //发送朋友圈数量
        long pyqTotolCount = 0;
        //悠哉精选数量
        long pyqYzjxCount = 0;
        //发圈的机器人数量
        long sendDeviceTotolCount = 0;
        //悠哉精选的机器人数量
        long sendDeviceYzjxCount = 0;
        //悠哉精选淘宝的机器人数量
        long sendDeviceYzjxCount_tb = 0;
        //悠哉精选拼多多的机器人数量
        long sendDeviceYzjxCount_pdd = 0;
        //悠哉精选悠友圈的机器人数量
        long sendDeviceYzjxCount_user = 0;
        //自动跟发朋友圈数量
        long followTotolCount = 0;
        //自动跟发机器人数量
        long followDeviceCount = 0;

        //发送朋友圈数量
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if(pyqReportDto.getStartTime() != 0){
            boolQuery.must(QueryBuilders.rangeQuery("sendTime").gte(pyqReportDto.getStartTime()));
        }
        if(pyqReportDto.getEndTime() != 0){
            boolQuery.must(QueryBuilders.rangeQuery("sendTime").lte(pyqReportDto.getEndTime()));
        }
        searchSourceBuilder.query(boolQuery);
        //默认查询不能超于1万条，如果数据超过1万条，则设置如下参数
        searchSourceBuilder.trackTotalHits(true);

        //发送悠哉精选朋友圈数量
        SearchSourceBuilder searchSourceBuilder_yzjx = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery_yzjx = QueryBuilders.boolQuery();
        boolQuery_yzjx.must(QueryBuilders.termsQuery("type", Lists.newArrayList(2,3,10)));    //悠哉精选
        boolQuery_yzjx.must(QueryBuilders.termQuery("source",1));  //自动发圈

        if(pyqReportDto.getStartTime() != 0){
            boolQuery_yzjx.must(QueryBuilders.rangeQuery("sendTime").gte(pyqReportDto.getStartTime()));
        }
        if(pyqReportDto.getEndTime() != 0){
            boolQuery_yzjx.must(QueryBuilders.rangeQuery("sendTime").lte(pyqReportDto.getEndTime()));
        }
        searchSourceBuilder_yzjx.query(boolQuery_yzjx);
        //默认查询不能超于1万条，如果数据超过1万条，则设置如下参数
        searchSourceBuilder_yzjx.trackTotalHits(true);

        //悠哉精选淘宝机器人数量
        SearchSourceBuilder searchSourceBuilder_yzjx_tb = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery_yzjx_tb = QueryBuilders.boolQuery();
        boolQuery_yzjx_tb.must(QueryBuilders.termQuery("type", 2));    //悠哉精选淘宝
        boolQuery_yzjx_tb.must(QueryBuilders.termQuery("source",1));  //自动发圈

        if(pyqReportDto.getStartTime() != 0){
            boolQuery_yzjx_tb.must(QueryBuilders.rangeQuery("sendTime").gte(pyqReportDto.getStartTime()));
        }
        if(pyqReportDto.getEndTime() != 0){
            boolQuery_yzjx_tb.must(QueryBuilders.rangeQuery("sendTime").lte(pyqReportDto.getEndTime()));
        }
        searchSourceBuilder_yzjx_tb.query(boolQuery_yzjx_tb);
        //默认查询不能超于1万条，如果数据超过1万条，则设置如下参数
        searchSourceBuilder_yzjx_tb.trackTotalHits(true);

        //悠哉精选拼多多机器人数量
        SearchSourceBuilder searchSourceBuilder_yzjx_pdd = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery_yzjx_pdd = QueryBuilders.boolQuery();
        boolQuery_yzjx_pdd.must(QueryBuilders.termQuery("type", 3));    //悠哉精选淘宝
        boolQuery_yzjx_pdd.must(QueryBuilders.termQuery("source",1));  //自动发圈

        if(pyqReportDto.getStartTime() != 0){
            boolQuery_yzjx_pdd.must(QueryBuilders.rangeQuery("sendTime").gte(pyqReportDto.getStartTime()));
        }
        if(pyqReportDto.getEndTime() != 0){
            boolQuery_yzjx_pdd.must(QueryBuilders.rangeQuery("sendTime").lte(pyqReportDto.getEndTime()));
        }
        searchSourceBuilder_yzjx_pdd.query(boolQuery_yzjx_pdd);
        //默认查询不能超于1万条，如果数据超过1万条，则设置如下参数
        searchSourceBuilder_yzjx_pdd.trackTotalHits(true);

        //悠哉精选悠友圈机器人数量
        SearchSourceBuilder searchSourceBuilder_yzjx_user = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery_yzjx_user = QueryBuilders.boolQuery();
        boolQuery_yzjx_user.must(QueryBuilders.termQuery("type", 10));    //悠哉精选淘宝
        boolQuery_yzjx_user.must(QueryBuilders.termQuery("source",1));  //自动发圈

        if(pyqReportDto.getStartTime() != 0){
            boolQuery_yzjx_user.must(QueryBuilders.rangeQuery("sendTime").gte(pyqReportDto.getStartTime()));
        }
        if(pyqReportDto.getEndTime() != 0){
            boolQuery_yzjx_user.must(QueryBuilders.rangeQuery("sendTime").lte(pyqReportDto.getEndTime()));
        }
        searchSourceBuilder_yzjx_user.query(boolQuery_yzjx_user);
        //默认查询不能超于1万条，如果数据超过1万条，则设置如下参数
        searchSourceBuilder_yzjx_user.trackTotalHits(true);

        //自动跟发的机器人数量
        SearchSourceBuilder searchSourceBuilder_follow = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery_follow = QueryBuilders.boolQuery();
        boolQuery_follow.must(QueryBuilders.termQuery("source",2)); //自动跟发
        if(pyqReportDto.getStartTime() != 0){
            boolQuery_follow.must(QueryBuilders.rangeQuery("sendTime").gte(pyqReportDto.getStartTime()));
        }
        if(pyqReportDto.getEndTime() != 0){
            boolQuery_follow.must(QueryBuilders.rangeQuery("sendTime").lte(pyqReportDto.getEndTime()));
        }
        searchSourceBuilder_follow.query(boolQuery_follow);
        //默认查询不能超于1万条，如果数据超过1万条，则设置如下参数
        searchSourceBuilder_follow.trackTotalHits(true);

        //16个ES分区，分别进行统计
        for(int i = 0; i< 16; i++ ){
            String index  = EsIndexName.UZAI_PYQ_JOB_DETAIL  + "_" + i;

            //自动发圈数量
            long pyqTotolCount_index = elasticsearchUtilGroupMsg.searchTotal(index, searchSourceBuilder);
            pyqTotolCount = pyqTotolCount + pyqTotolCount_index;

            //悠哉精选淘宝数量
            long pyqYzjxCount_tb_index = elasticsearchUtilGroupMsg.searchTotal(index, searchSourceBuilder_yzjx);
            pyqYzjxCount = pyqYzjxCount + pyqYzjxCount_tb_index;

            //发圈的机器人数量(去重)
            long sendDeviceTotolCount_index = elasticsearchUtilGroupMsg.countDistinctField(index, "deviceUniqueId", searchSourceBuilder);
            sendDeviceTotolCount = sendDeviceTotolCount + sendDeviceTotolCount_index;

            //悠哉精选的机器人数量(去重)
            long sendDeviceYzjxCount_index = elasticsearchUtilGroupMsg.countDistinctField(index, "deviceUniqueId", searchSourceBuilder_yzjx);
            sendDeviceYzjxCount = sendDeviceYzjxCount + sendDeviceYzjxCount_index;

            //悠哉精选淘宝的机器人数量(去重)
            long sendDeviceYzjxCount_tb_index = elasticsearchUtilGroupMsg.countDistinctField(index, "deviceUniqueId", searchSourceBuilder_yzjx_tb);
            sendDeviceYzjxCount_tb = sendDeviceYzjxCount_tb + sendDeviceYzjxCount_tb_index;

            //悠哉精选拼多多的机器人数量(去重)
            long sendDeviceYzjxCount_pdd_index = elasticsearchUtilGroupMsg.countDistinctField(index, "deviceUniqueId", searchSourceBuilder_yzjx_pdd);
            sendDeviceYzjxCount_pdd = sendDeviceYzjxCount_pdd + sendDeviceYzjxCount_pdd_index;

            //悠哉精选悠友圈的机器人数量(去重)
            long sendDeviceYzjxCount_user_index = elasticsearchUtilGroupMsg.countDistinctField(index, "deviceUniqueId", searchSourceBuilder_yzjx_user);
            sendDeviceYzjxCount_user = sendDeviceYzjxCount_user + sendDeviceYzjxCount_user_index;

            //自动跟发朋友圈数量
            long followTotolCount_index = elasticsearchUtilGroupMsg.searchTotal(index, searchSourceBuilder_follow);
            followTotolCount = followTotolCount + followTotolCount_index;

            //自动跟发机器人数量(去重)
            long followDeviceCount_index = elasticsearchUtilGroupMsg.countDistinctField(index, "deviceUniqueId", searchSourceBuilder_follow);
            followDeviceCount = followDeviceCount + followDeviceCount_index;

        }

        //自动发圈数量
        pyqReportVo.setPyqTotolCount(pyqTotolCount);
        //悠哉精选数量
        pyqReportVo.setPyqYzjxCount(pyqYzjxCount);
        //发圈的机器人数量(去重)
        pyqReportVo.setSendDeviceTotolCount(sendDeviceTotolCount);
        //悠哉精选的机器人数量(去重)
        pyqReportVo.setSendDeviceYzjxCount(sendDeviceYzjxCount);
        //悠哉精选淘宝的机器人数量(去重)
        pyqReportVo.setSendDeviceYzjxTbCount(sendDeviceYzjxCount_tb);
        //悠哉精选拼多多的机器人数量(去重)
        pyqReportVo.setSendDeviceYzjxPddCount(sendDeviceYzjxCount_pdd);
        //悠哉精选悠友圈的机器人数量(去重)
        pyqReportVo.setSendDeviceYzjxUserCount(sendDeviceYzjxCount_user);
        //自动跟发朋友圈数量
        pyqReportVo.setFollowTotolCount(followTotolCount);
        //自动跟发机器人数量(去重)
        pyqReportVo.setFollowDeviceCount(followDeviceCount);

        //社群群发统计
        long fdSendDeviceCount = 0;
        //社群手动发送机器人数量
        long fdHanderSendDeviceCount= 0;;
        //社群自动跟发机器人数量
        long fdFollowSendDeviceCount= 0;;
        //社群跟发官方采集群机器人数量
        long fdFollowGroupCount= 0;

        //查询社群群发统计
        FdSendJobQuery fdSendJobQuery_total = new FdSendJobQuery();
        fdSendJobQuery_total.setQueryStartTime(pyqReportDto.getStartTime());
        fdSendJobQuery_total.setQueryEndTime(pyqReportDto.getEndTime());
        fdSendDeviceCount = fdSendJobMapper.findDeviceCount(fdSendJobQuery_total);

        //查询社群手动发送机器人数量
        FdSendJobQuery fdSendJobQuery_hander = new FdSendJobQuery();
        fdSendJobQuery_hander.setSource(0);//手动发送
        fdSendJobQuery_hander.setQueryStartTime(pyqReportDto.getStartTime());
        fdSendJobQuery_hander.setQueryEndTime(pyqReportDto.getEndTime());
        fdHanderSendDeviceCount = fdSendJobMapper.findDeviceCount(fdSendJobQuery_hander);

        //查询社群自动跟发机器人数量
        FdSendJobQuery fdSendJobQuery_follow = new FdSendJobQuery();
        fdSendJobQuery_follow.setSourceList(Lists.newArrayList(2,3));    //手动发送
        fdSendJobQuery_follow.setQueryStartTime(pyqReportDto.getStartTime());
        fdSendJobQuery_follow.setQueryEndTime(pyqReportDto.getEndTime());
        fdFollowSendDeviceCount = fdSendJobMapper.findDeviceCount(fdSendJobQuery_follow);

        //社群跟发官方采集群机器人数量(只能通过查询开启的任务总查询机器人数量)
        Set<Long> deviceUniqueIdSet = new HashSet<>();
        //查询所有的运营商采集群开启的任务
        FdFollowGroupConfigQuery fdFollowGroupConfigQuery = new FdFollowGroupConfigQuery();
        fdFollowGroupConfigQuery.setStatus(1); //启动状态
        List<FdFollowGroupConfig> fdFollowGroupConfigList = fdFollowGroupConfigMapper.findByList(fdFollowGroupConfigQuery);
        if(fdFollowGroupConfigList != null && fdFollowGroupConfigList.size() > 0){
            for (FdFollowGroupConfig fdFollowGroupConfig : fdFollowGroupConfigList) {
                //查询该配置方案关联的群信息
                List<FdDevicePlanGroup> fdDevicePlanGroupList = fdDevicePlanGroupMapper.findByListByFdDevicePlanId(fdFollowGroupConfig.getFdDevicePlanId(), fdFollowGroupConfig.getMerId());
                if (fdDevicePlanGroupList != null && fdDevicePlanGroupList.size() > 0) {
                    for (FdDevicePlanGroup fdDevicePlanGroup : fdDevicePlanGroupList) {
                        deviceUniqueIdSet.add(fdDevicePlanGroup.getGroupSendDeviceUniqueId());
                    }
                }
            }
        }
        fdFollowGroupCount = deviceUniqueIdSet.size();

        //社群群发统计
        pyqReportVo.setFdSendDeviceCount(fdSendDeviceCount);
        //社群手动发送机器人数量
        pyqReportVo.setFdHanderSendDeviceCount(fdHanderSendDeviceCount);
        //社群自动跟发机器人数量
        pyqReportVo.setFdFollowSendDeviceCount(fdFollowSendDeviceCount);
        //社群跟发官方采集群机器人数量
        pyqReportVo.setFdFollowGroupCount(fdFollowGroupCount);

        return pyqReportVo;
    }


}
